import request from "./request";

export type Todos = Todo[];

export interface Todo {
    userId: number;
    id: number;
    title: string;
    completed: boolean;
}

export type AddTodoType = Omit<Todo, "id">;

type KeyofTodo = keyof Todo;

export type UpdateTodoType = Partial<Pick<Todo, Exclude<KeyofTodo, "id">>>;

export type TodoPreview = Pick<Todo, "id" | "title" | "completed">;

export function fetchTodes() {
    return request.get<Todos>("/todos");
}

export function fetchTode(id: number) {
    return request.get<Todo>(`/todos/${id}`);
}

export function addTodo(todo: AddTodoType) {
    return request.post<Todo>("/todos", todo);
}

export function deleteTodo(id: number) {
    return request.delete<unknown>(`/todos/${id}`);
}

export function updateTodo(id: number, todo: UpdateTodoType) {
    return request.put<Todo>(`/todos/${id}`, todo);
}